package com.yidian.controller.business;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yidian.entity.Result;
import com.yidian.pojo.order.Order;
import com.yidian.pojo.user.Address;
import com.yidian.server.order.CartService;
import com.yidian.server.order.OrderService;
import com.yidian.server.user.AddressService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author whb
 * @create 2021-08-26 10:16
 * @desciption 购物车控制类
 */
@RestController
@RequestMapping("/cart")
public class CartController {
    @Reference
    private CartService cartService;
    @Reference
    private AddressService addressService;
    @Reference
    private OrderService orderService;

    @GetMapping("/findCartList")
    public List<Map<String,Object>> findCartList(){
        String username= SecurityContextHolder.getContext().getAuthentication().getName();//通过Springsecurity CAS获取登录用户名
        List<Map<String,Object>> cartList=cartService.findCartList(username);
        return cartList;
    }

    @GetMapping("/addOrderItem")
    public Result addOrderItem(String skuId,Integer num){
        String username= SecurityContextHolder.getContext().getAuthentication().getName();
        cartService.addItem(username,skuId,num);
        return new Result(0,"添加商品到购物车");
    }

    /**
     * 商品详细页加入购物车
     * @param response
     * @param skuId 商品skuId
     * @param num 商品数量
     */
    @RequestMapping("/buy")
    public void buy(HttpServletResponse response,String skuId,Integer num) throws IOException {
        String username=SecurityContextHolder.getContext().getAuthentication().getName();
        cartService.addItem(username,skuId,num);
        response.sendRedirect("/cart.html");
    }

    /**
     * 更新购物车商品勾选状态
     * @param skuId
     * @param checked
     * @return
     */
    @GetMapping("/updateChecked")
    public Result updateChecked(String skuId,boolean checked){
        String username=SecurityContextHolder.getContext().getAuthentication().getName();
        cartService.updateChecked(username,skuId,checked);
        return new Result(0,"商品勾选状态已更新");
    }

    /**
     * 删除选中的商品
     * @return
     */
    @GetMapping("/deleteCheckedCart")
    public Result deleteCheckedCart(){
        String username=SecurityContextHolder.getContext().getAuthentication().getName();//通过Springsecurity CAS获取登录用户名
        cartService.deleteCheckedCart(username);
        return new Result(0,"删除选中的商品");
    }

    /**
     * 计算当前购物车中已选中商品的优惠金额
     * @return
     */
    @GetMapping("/preMoney")
    public Map preMoney(){
        String username=SecurityContextHolder.getContext().getAuthentication().getName();
        int preferential = cartService.preferential(username);
        Map map=new HashMap();
        map.put("preMoney",preferential);
        return map;
    }

    /**
     * 获取最新的购物车列表（购物车商品价格和数据库商品价格同步）
     * @return
     */
    @GetMapping("/findNewCartList")
    public List<Map<String,Object>> findNewCartList(){
        String username=SecurityContextHolder.getContext().getAuthentication().getName();
        return cartService.findNewOrderItemList(username);
    }

    /**
     * 根据用户名查询收获地址列表
     * @return
     */
    @GetMapping("/findAddressList")
    public List<Address> findAddressList(){
        String username=SecurityContextHolder.getContext().getAuthentication().getName();
        return addressService.findByUsername(username);
    }

    /**
     * 保存订单
     * @param order
     * @return
     */
    @PostMapping("/saveOrder")
    public Map<String,Object> saveOrder(@RequestBody Order order){
        String username=SecurityContextHolder.getContext().getAuthentication().getName();
        order.setUsername(username);
        return orderService.addOrder(order);
    }
}
